package com.onpositive.units;

import java.util.Collection;
import java.util.Collections;

import org.w3c.dom.Element;

import com.onpositive.knowledge.model.values.IHasValue;

public class ParsedIdent extends ParsedTerm {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4087514431379961840L;
	
	public static final String ID = "ident" ;

	private final String ref ;
	private final String caption ;
	
	public ParsedIdent( String value, String comment)
	{
		super(comment);
		
		String[] arr = initMembers(value);
		ref = arr[0] ;
		caption = arr[1] ;
	}
	
	public ParsedIdent( ParsedValue pv, String comment )
	{
		super( comment ) ;		
		ParsedIdent other = (ParsedIdent) pv ;
		this.ref = other.ref ;
		this.caption = other.caption ;
	}
	
	public ParsedIdent( Element element ) {
		super(element);
		String value = element.getAttribute("value") ;		
		String[] arr = initMembers(value);
		ref = arr[0] ;
		caption = arr[1] ;
	}
	
	public ParsedIdent(String ref, String caption, String comment )
	{
		super(comment) ;
		this.ref = ref ;
		this.caption = caption ;
	}

	private String[] initMembers(String _value)
	{
		String[] arr = new String[2] ;
		
		if( _value == null){
			arr[0] = null ;
			arr[1] = null ;
			return arr ;
		}
		
		String value = _value.trim() ;
		if( value.startsWith("[[") )
			value = value.substring(2).trim() ;
		
		if( value.endsWith("]]") )
			value = value.substring( 0 , value.length()-2 ).trim() ;		
		
		int l = value.length();
		int ind = value.indexOf("|") ;
			
		if( ind < 0 ){
			arr[0] = value.length() != 0 ? value : null ;
			arr[1] = null ;
			return arr;
		}	
		String refStr = value.substring(0, ind).trim();
		String capStr = value.substring(ind+1, l).trim();
		
		arr[0] = refStr.length() != 0 ? refStr : null ;		
		arr[1] = capStr.length() != 0 ? capStr : null ;

		return arr;
	}

	@Override
	public Object value() {
		return getValueAsString();
	}

	@Override
	public Object describer() {
		return String.class;
	}

	@Override
	public String getId() {
		return ID;
	}
	
	@Override
	public String getValueAsString() {
		StringBuilder bld = new StringBuilder() ;
		bld.append("[[") ;
		bld.append( ref != null ? ref : "" ) ;
		bld.append("|") ;
		bld.append(caption != null ? caption : (ref!=null?ref:"") ) ;
		bld.append("]]") ;
		return bld.toString() ;
	}
	public String getRef() {
		return ref;
	}

	public String getCaption() {
		return caption;
	}

	@Override
	public void setValue(Object obj) {
		
	}	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((caption == null) ? 0 : caption.hashCode());
		result = prime * result + ((ref == null) ? 0 : ref.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ParsedIdent other = (ParsedIdent) obj;
		if (caption == null) {
			if (other.caption != null)
				return false;
		} else if (!caption.equals(other.caption))
			return false;
		if (ref == null) {
			if (other.ref != null)
				return false;
		} else if (!ref.equals(other.ref))
			return false;
		return true;
	}

	@Override
	public IHasValue clone(Object value, String comment) {
		if(!(value instanceof String))
			return null ;
		
		if( comment == null )
			comment = this.getComment() ;
		
		return new ParsedIdent((String) value, comment );
	}

	

	
}
